import { ErrorLocalCode } from '../defines/error.local.define';
import { IPlatform } from '../interface/platform.interface';
import flyio from 'flyio';
export class PlatformPhoenix implements IPlatform {
    public setBaseUrl(url: string) {
        flyio.config.baseURL = url;
        flyio.config.timeout = 50000;
        flyio.interceptors.request.use((config) => {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            return config;
        });
    }

    async login(cb: Function): Promise<string | boolean> {
        return '';
    }

    public async request(method, url, data, token?): Promise<any> {
        return new Promise((resolve, reject) => {
            if (token) {
                flyio
                    .request(url, data, {
                        method: method,
                        headers: {
                            Authorization: `Bearer ${token}`,
                        },
                    })
                    .then((res) => {
                        resolve(res.data);
                    })
                    .catch((err) => {
                        if (err.response) {
                            reject(err.response.data);
                        } else {
                            reject({ code: ErrorLocalCode.REQUEST_FAILED, message: err.message });
                        }
                    });
            } else {
                flyio
                    .request(url, data, {
                        method: method,
                    })
                    .then((res) => {
                        resolve(res.data);
                    })
                    .catch((err) => {
                        if (err.response) {
                            reject(err.response.data);
                        } else {
                            console.log('err.message:', err);
                            reject({ code: ErrorLocalCode.REQUEST_FAILED, message: err.message });
                        }
                    });
            }
        });
    }
}

